package cn.gson.springboot.model.service;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.gson.springboot.model.dao.OrderClassDao;
import cn.gson.springboot.model.mapper.SectionMapper;
import cn.gson.springboot.model.mapper.TriageMapper;
import cn.gson.springboot.model.mapper.UserMapper;
import cn.gson.springboot.model.pojos.OrderClass;
import cn.gson.springboot.model.pojos.Section;
import cn.gson.springboot.model.pojos.User;
import cn.gson.springboot.model.vo.OrderClassVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class TriageService {
	@Autowired
	 private SectionMapper sectionmapper;
	
	@Autowired
	 private TriageMapper tmapper;
	
	@Autowired
	private UserMapper um;
	
	//医疗卡的service	
	@Autowired
	private ClinicCardService  ccs;
	
	//排班记录表dao
	@Autowired
	private OrderClassDao orderclassdao;
	
     //初始化表格数据
	 public JSONArray  selectAlltriage(Integer registr_state,String clinicname) {
		 return tmapper.selectAlltriage(registr_state,clinicname);
	 }
	 
	  public JSONArray  selectAlldoctor(/*Integer sectionid,*/String sectionname) {
		  return tmapper.selectAlldoctor(/*(sectionid,*/sectionname);
	  }
	  
	 //查出当天排班科室
	 public JSONArray  selectAllPresentOrderSection(Date orderclass_time) {
		  return tmapper.selectAllPresentOrderSection(orderclass_time);
	 }
	 
	//查出当天排班科室查出人员
	public JSONArray  selectAllPresentOrderPeople(Date  orderclass_time,String sectionname,Integer section_id,String rolename,Time arrange_starclassses, Time arrange_endclassses,Integer arrange_classsid) {
		  return tmapper.selectAllPresentOrderPeople(orderclass_time, sectionname, section_id, rolename, arrange_starclassses, arrange_endclassses, arrange_classsid);
	}
	
	 //查询这些科室中的医生护士,初始化科室的下拉列表
	  public JSONArray selectUserSectionArrange() {
		  return tmapper.selectUserSectionArrange();
	  }
	
	 //科室下面对应人员初始化
    public JSONArray selectAllsectionPeople(Integer sectionid,String role_name) {
	   return tmapper.selectAllsectionPeople(sectionid,role_name);
    }
    
  //安排排班新增 
    public void addOrder_class(Integer arrange_classsid,Integer user_id,Date orderclass_time) {
    	tmapper.addOrder_class(arrange_classsid, user_id, orderclass_time);
    }
    
  //根据开始与结束时间查到安排的id
    public JSONArray  selectarrange_classsid(Time arrange_starclassses,Time arrange_endclassses) {
    	return tmapper.selectarrange_classsid(arrange_starclassses, arrange_endclassses);
    }
    
    //查询排班时间
    public JSONArray selectarrange_time(){
    	return tmapper.selectarrange_time();
    }
    
    //<!-- 根据用户id查询角色 -->
    public JSONArray selectuser_role(@Param("userid") Integer  userid) {
    	return tmapper. selectuser_role(userid);
    }
    
    //<!-- 根据角色查询排班班次 -->
    public JSONArray selectarrange_classes(@Param("arrange_role") String  arrange_role) {
    	return tmapper.selectarrange_classes(arrange_role);
    }
    
    //<!-- 新增排班判断-->
    public JSONArray IfOrderPeople( Date  orderclass_time,Integer sectionid, Integer userid,Integer arrange_classsid) {
    	return tmapper.IfOrderPeople(orderclass_time, sectionid, userid,arrange_classsid);
    }
    
    //修改排班班次1 
    public void updateOrderclass( Integer arrange_classsid,Date orderclass_time,Integer orderclass_id) {
    	tmapper.updateOrderclass(arrange_classsid, orderclass_time, orderclass_id);
    }
    
    //新增排班
    public void add_Orderclass(String orderclass_startime,Integer user_id,String orderclass_time) throws ParseException {
     	System.out.println("排班时间段："+orderclass_startime);
     	System.out.println("排班ren ："+user_id);
    	System.out.println("排班日期 ："+orderclass_time);
    	/*String order_startime=orderclass_startime.substring(0, 5);
    	String startime=order_startime+":00";
    	//System.out.println("排班开始时间："+order_startime);
    	String order_endtime=orderclass_startime.substring(6,11);
    	String endtime=order_endtime+":00";
    	//System.out.println("排班结束时间："+order_endtime+"---------------"+endtime);
 
    	//根据开始与结束时间查到安排的id
    	JSONArray selectarrange_classsid = tmapper.selectarrange_classsid(Time.valueOf(startime), Time.valueOf(endtime));
    	Integer arrange_classsid = selectarrange_classsid.getJSONObject(0).getInteger("arrange_classsid");
    	System.out.println("arrange_classsid："+arrange_classsid);*/
    	tmapper.addOrder_class(Integer.valueOf(orderclass_startime), user_id,java.sql.Date.valueOf(orderclass_time));
	    }
    
	//根据排班科室查出当天【早班】排班人员
	 public JSONArray  selectAllPresentMorningOrderPeople(String   orderclass_time,String sectionname,String rolename,String arrangetime) throws ParseException {
		   // System.out.println("早班排班科室--------时间---------:"+sectionname+"---------"+orderclass_time);
		   //排班日期
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		  Date orderclasstime= formatter.parse(orderclass_time);
		  //早班班次【开始时间】与【结束时间】
		 /* String order_startime=arrangetime.substring(0, 5);
	      String order_endtime=arrangetime.substring(6,11);
		  String arrange_starclassses =order_startime+":00";
		  String arrange_endclassses =order_endtime+":00";*/
          // System.out.println("arrange_starclassses:"+arrange_starclassses+"arrange_endclassses:"+arrange_endclassses);
		  //根据【开始时间】与【结束时间】找到排班人
		  /*JSONArray selectAllPresentOrderPeople = tmapper.selectAllPresentOrderPeople(orderclasstime,null,Integer.valueOf(sectionname),rolename,
				  																		   Time.valueOf(arrange_starclassses),Time.valueOf(arrange_endclassses));*/
		  JSONArray selectAllPresentOrderPeople = tmapper.selectAllPresentOrderPeople(orderclasstime,null,Integer.valueOf(sectionname),rolename,
					   null,null,Integer.valueOf(arrangetime));
		
		 //System.out.println("早班排班人员-----------------:"+selectAllPresentOrderPeople);
		 
		  
		  return selectAllPresentOrderPeople;
	 }
	 
 //根据排班科室查出当天【中班】排班人员
    public JSONArray  selectAllPresentMiddleOrderPeople(String  orderclass_time,String sectionname,String rolename,String arrangetime) throws ParseException {
	    //System.out.println("中班排班科室-----------------:"+sectionname);
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		//排班日期
	    Date orderclasstime=formatter.parse(orderclass_time);
	    
	    //早班班次【开始时间】与【结束时间】
		 /* String order_startime=arrangetime.substring(0, 5);
	      String order_endtime=arrangetime.substring(6,11);
		  String middle_starclassses =order_startime+":00";
		  String middle_endclassses =order_endtime+":00";*/
		
	   /* //首先查询早班班次【开始时间】与【结束时间】
	   JSONArray selectarrange_time = tmapper.selectarrange_time();
	   String middle_starclassses = selectarrange_time.getJSONObject(1).getString("arrange_starclassses");
	   String middle_endclassses = selectarrange_time.getJSONObject(1).getString("arrange_endclassses");*/

		//根据【开始时间】与【结束时间】找到排班人
	   /* JSONArray selectAllPresentOrderPeople = tmapper.selectAllPresentOrderPeople(orderclasstime,null,Integer.valueOf(sectionname),rolename,
			  																		   Time.valueOf(middle_starclassses),Time.valueOf(middle_endclassses));*/
		  JSONArray selectAllPresentOrderPeople = tmapper.selectAllPresentOrderPeople(orderclasstime,null,Integer.valueOf(sectionname),rolename,
					   null,null,Integer.valueOf(arrangetime));
		
	
	   return selectAllPresentOrderPeople;
   }
    
   //根据排班科室查出当天【晚班】排班人员
   public JSONArray  selectAllPresentNightOrderPeople(String  orderclass_time,String sectionname,String rolename,String arrangetime) throws ParseException {
		   //System.out.println("晚班排班科室-----------------:"+sectionname);
		    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Date orderclasstime= formatter.parse(orderclass_time);
			
		   /* String order_startime=arrangetime.substring(0, 5);
	        String order_endtime=arrangetime.substring(6,11);
		    String end_starclassses =order_startime+":00";
		    String end_endclassses =order_endtime+":00";*/
			
		   /*//首先查询早班班次【开始时间】与【结束时间】
		   JSONArray selectarrange_time = tmapper.selectarrange_time();
		   String end_starclassses = selectarrange_time.getJSONObject(2).getString("arrange_starclassses");
		   String end_endclassses = selectarrange_time.getJSONObject(2).getString("arrange_endclassses");*/

		    //根据【开始时间】与【结束时间】找到排班人
		   /* JSONArray selectAllPresentOrderPeople = tmapper.selectAllPresentOrderPeople(orderclasstime,null,Integer.valueOf(sectionname),rolename,
				  																		   Time.valueOf(end_starclassses),Time.valueOf(end_endclassses));*/
		    JSONArray selectAllPresentOrderPeople = tmapper.selectAllPresentOrderPeople(orderclasstime,null,Integer.valueOf(sectionname),rolename,null,null,Integer.valueOf(arrangetime));
		     return selectAllPresentOrderPeople;
  	 }
   
    //排班新增权限控制
   public String IfarrangeAdd(HttpSession session){
      	 //获取登录人
	     User user = (User)session.getAttribute("user");	
	     //System.out.println("排班---直接获取登录人id："+user.getUserId());
	     //根据登录人的id查出登录人的角色
	     JSONArray selectLookpeople = ccs.selectLookDoctorBysid(null, user.getUserId());
	     //System.out.println(selectLookpeople.size()+"排班------------根据登录人的id查出登录人的角色:"+selectLookpeople.getJSONObject(0).getString("role_name"));
         //根据登录人的角色进行判断
	     String rolename = selectLookpeople.getJSONObject(0).getString("role_name");
	     //System.out.println("排班---登录人的角色："+rolename);
	     //登录人为高级领导人：院长或是副院长
	     if(rolename.equals("院长") || rolename.equals("副院长")) {
	    	 return "1";
	     }
	     return "0";
   }
   
   //修改排班班次
   public void updateArrange_class(Integer arrange_classsid,Time arrange_starclassses,Time arrange_endclassses) {
	   tmapper.updateArrange_class(arrange_classsid, arrange_starclassses, arrange_endclassses);
   }
	
	 // 查询排班日期【前一天】或者【后一天】的排班科室
	public JSONArray selectbefore_AfterOrderSection( Date  orderclass_time){
		  return tmapper.selectbefore_AfterOrderSection(orderclass_time);
	}
		  
		  
	//根据科室查询排班日期【前一天】或者【后一天】的人 
	public JSONArray  selectbefore_AfterOrderPeople(Date  orderclass_time,String sectionname,
																									 Time arrange_starclassses,Time arrange_endclassses) {
		return tmapper.selectbefore_AfterOrderPeople(orderclass_time, sectionname, arrange_starclassses, arrange_endclassses);
	} 
	
	 //根据科室名称找到当天对应的角色
	  public JSONArray selectAllPresentRolename(String   orderclass_time,String sectionname) throws ParseException {
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		  Date orderclasstime= formatter.parse(orderclass_time);
		  JSONArray selectAllPresentRolename = tmapper.selectAllPresentRolename(orderclasstime, sectionname);
		  return selectAllPresentRolename;
	  }
	  
	   //查询所有科室
	  public JSONArray  selectAll_ArrangeSection() {
		  return tmapper.selectAll_ArrangeSection();
	  }
	  
	  //根据开始排班班次查结束
	  public JSONArray arrangeclasses_endclass(Integer arrange_classsid) {
		  return tmapper.arrangeclasses_endclass(arrange_classsid);
	  }
	  
	  //多人新增排班
	  public void addMang_Order_class(OrderClassVo arrangeclassvo) {
		  List<OrderClass> orderclass = arrangeclassvo.getOrderclass();
		  for (OrderClass order_class : orderclass) {
			  
			  //新增排班
			  orderclassdao.save(order_class);
		}
	  }
	  
	  //根据角色查询排班班次初始化排班班次
	  public JSONArray init_arrange_classes( String arrange_role) {
		  return tmapper.init_arrange_classes(arrange_role);
	  }
	  
	  public JSONArray selectAll_Sectionmanage(String section_name) {
		  return tmapper.selectAll_Sectionmanage(section_name);
	  }
	  
	//查询所有班次 
    public JSONArray selectAll_arrange_classes(String arrange_role,String arrange_classses) {
    	return tmapper. selectAll_arrange_classes(arrange_role,arrange_classses);
    }

}
